<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 用户管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="danger"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    :disabled="multipleSelection.length <= 0"
                    @click="delAllSelection()"
                >批量删除</el-button>
                <el-input v-model="content" clearable placeholder="请输入..." class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="getUserInfo">搜索</el-button>
                <el-button icon="el-icon-refresh-right" @click="Reset" type="success" size="medium" circle></el-button> 
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="code" label="邀请码" align="center"></el-table-column>
                <el-table-column prop="consignee" label="收货人" align="center"></el-table-column>
                <el-table-column prop="address" label="收货地址" align="center"></el-table-column>
                <el-table-column prop="phone" label="收货人联系电话" align="center"></el-table-column>
                <el-table-column label="累计消费金额" align="center">
                    <template slot-scope="scope">￥{{scope.row.totalMoney}}</template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="delAllSelection(scope.row.id)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                background
                @size-change="sizeChangeHandle"
                @current-change="currentChangeHandle"
                :current-page="pageIndex"
                :page-sizes="[5 , 10]"
                :page-size="pageSize"
                :total="totalPage"
                layout="total, sizes, prev, pager, next, jumper">
                </el-pagination>
            </div>
        </div>
        <!-- -->
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="商品ID">
                    <el-input v-model="form.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="用户名">
                    <el-input v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item label="邀请码">
                    <el-input v-model="form.code"></el-input>
                </el-form-item>
                <el-form-item label="收货人">
                    <el-input v-model="form.consignee"></el-input>
                </el-form-item>
                <el-form-item label="收货地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
                <el-form-item label="收货人联系电话">
                    <el-input v-model="form.phone"></el-input>
                </el-form-item>
                <el-form-item label="累计消费金额">
                    <el-input v-model="form.totalMoney"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { mapActions } from "vuex";
import { fetchData } from '../../api/index';
import axios from 'axios'
import * as url from '../../http/api'
import http from '../../http/http'
const imgUrl = 'http://47.108.197.149:8080/images/'
export default {
    name: 'goodstable',
    data() {
        return {
            imageUrl: 'http://47.108.197.149:8081/file/upload', //上传商品图像的url
            imgUrl : 'http://47.108.197.149:8080/images/',
            tableData: [],    //表数据
            multipleSelection: [],  //多选数组
            delList: [],
            editVisible: false,
            addVisible: false,  //新增dialog是否显示
            pageIndex: 1,
            pageSize: 10,
            totalPage: 0,
            form: {},
            idx: -1,
            id: -1,
            addFrom:{},  //新增表单
            image:'',
            selectContent:'',  //选择搜索内容
            content:'',  //搜索内容,
        };
    },
    created() {
        this.getUserInfo();
    },
    methods: {
        ...mapActions(["mmm"]),
        getUserInfo(){  //获取商品、选择分类、搜索商品、重置的统一方法
            http({
                url:url.getUserList,
                method:'get',
                params:{
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'key':this.content
                }
            }).then(res =>{
                console.log(res)
                this.tableData = res.data.page.list
                this.totalPage = res.data.page.totalCount
            }).catch(err =>{
                console.log(err)
            })
        },
        Reset(){   //重置按钮的方法
            http({
                url:url.getUserList,
                method:'get',
                params:{
                    'page': this.pageIndex,
                    'limit': this.pageSize,
                    'type':0,
                    'key':''
                }
            }).then(res =>{
                this.tableData = res.data.page.list
                this.totalPage = res.data.page.totalCount
                this.content = ''
            }).catch(err =>{
                console.log(err)
            })
        },
         // 每页数
        sizeChangeHandle (val) {
            this.pageSize = val
            this.pageIndex = 1
            this.getUserInfo()
        },
        // 当前页
        currentChangeHandle (val) {
            this.pageIndex = val
            this.getUserInfo()
        },
        // 多选(删除)操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection(id) {      //删除
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].name + ' ';
            }
            // this.multipleSelection = [];
            let ids = id ? [id] : this.multipleSelection.map(item => {
                return item.id
            })
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$message.error(`删除了${str}`);
                    http({
                        url:url.deleteUser,
                        method:'post',
                        data:{
                            ids:ids
                        }
                    }).then(res =>{
                        this.getUserInfo()
                        console.log(res)
                    }).catch(err =>{
                        console.log(err)
                    })
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            http({
                url:url.updateUser,
                method:'post',
                data:{
                    id:this.form.id,
                    wxUsername:this.form.wxUsername,
                    code:this.form.code,
                    consignee:this.form.consignee,
                    address:this.form.address,
                    phone:this.form.phone,
                    totalMoney:this.form.totalMoney,
                }
            }).then(res =>{
                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                this.getUserInfo()
            }).catch(err =>{
                console.log(err)
            })

        },
        // 分页导航
        handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.image = res.image
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
